//
//  CityTravelViewController.m
//  TravelFollowYourHeart
//
//  Created by ENERGY on 2017/12/18.
//  Copyright © 2017年 Lwx. All rights reserved.
//

#import "CityTravelViewController.h"
#import "CityTravelTableViewCell.h"
#import "NewTravelNoteViewController.h"
#import "SearchTravelViewController.h"
#import "CityTravelModel.h"
#import "TravelsViewController.h"

@interface CityTravelViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UIButton * searchBtn;
@property (nonatomic, strong)UIButton * plusBtn;
@property (nonatomic, strong)UIView * topView;
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)UIView * leftView;
@property (nonatomic, strong)CityTravelModel * model;
@property (nonatomic, assign)int currentPage;
@property (nonatomic, strong)NSMutableArray * dataArr;
@property (nonatomic, assign)int travelID;
@property (nonatomic, assign)BOOL isPopular;

@end

@implementation CityTravelViewController(Networking)
- (void)getCityTravelData{
    NSDictionary * dic = @{
                           @"rule":self.isPopular?@(1):@(2),
                           @"page":@(self.currentPage),
                           @"row":@(6)
                           };
    WS(ws)
    [[HttpRequest shardWebUtil] postNetworkRequestURLString:Url(@"/diary/diary") andParas:dic andTransferGottenData:^(id obj, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (error) {
            [[HUDHelper sharedInstance]tipMessage:obj[@"msg"]];
        }else{
            if ([obj[@"code"] integerValue] == 100000) {
                if (ws.currentPage == 1) {
                    [ws.dataArr removeAllObjects];
                }
                ws.model = [CityTravelModel yy_modelWithDictionary:obj];
                [ws.dataArr addObjectsFromArray:ws.model.data];
                [ws.tableView reloadData];
            }else{
                [[HUDHelper sharedInstance]tipMessage:obj[@"msg"]];
            }
        }
    }];

}

@end

@implementation CityTravelViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.dataArr = [NSMutableArray array];
    [self getCityTravelData];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _isPopular = YES;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.searchBtn];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.plusBtn];
    [self.view addSubview:self.topView];
    [self.view addSubview:self.tableView];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
}

- (UIButton *)searchBtn{
    if (!_searchBtn) {
        _searchBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 42 / 2, 40 / 2)];
        [_searchBtn setBackgroundImage:[UIImage imageNamed:@"xcyj_search"] forState:UIControlStateNormal];
        [_searchBtn addTarget:self action:@selector(searchTravelPage) forControlEvents:UIControlEventTouchUpInside];
//        _searchBtn.adjustsImageWhenHighlighted = NO;
    }return _searchBtn;
}

- (UIButton *)plusBtn{
    if (!_plusBtn) {
        _plusBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 42 / 2, 40 / 2)];
        [_plusBtn setBackgroundImage:[UIImage imageNamed:@"xcyj_xinjian"] forState:UIControlStateNormal];
        [_plusBtn addTarget:self action:@selector(addTravelNotePage) forControlEvents:UIControlEventTouchUpInside];
    }return _plusBtn;
}

- (UIView *)topView{
    if (!_topView) {
        _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100 / 2)];
        _topView.backgroundColor = [UIColor whiteColor];
        
        //右按钮
        UIButton * popularityBtn = [[UIButton alloc] init];
        [popularityBtn setTitle:@"人气" forState:UIControlStateNormal];
        popularityBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [popularityBtn setTitleColor:getColor(@"6d6d6d") forState:UIControlStateNormal];
        [popularityBtn addTarget:self action:@selector(clickMove:) forControlEvents:UIControlEventTouchUpInside];
        popularityBtn.frame = CGRectMake(0, 0, SCREEN_WIDTH / 2, _topView.height);
        [_topView addSubview:popularityBtn];
        
        int leftViewX = 38;
        int lineViewW = SCREEN_WIDTH / 2 - 2 * leftViewX;
        int lineViewH = 3;
        int lineViewY = _topView.bottom - 3;
        self.leftView = [[UIView alloc] init];
        self.leftView.backgroundColor = getColor(@"ffbb02");
        self.leftView.frame = CGRectMake(leftViewX, lineViewY, lineViewW, lineViewH);
        [self.topView addSubview:self.leftView];
        
        //左按钮
        UIButton * timeBtn = [[UIButton alloc] init];
        [timeBtn setTitle:@"时间" forState:UIControlStateNormal];
        timeBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [timeBtn setTitleColor:getColor(@"6d6d6d") forState:UIControlStateNormal];
        timeBtn.frame = CGRectMake(SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2, _topView.height);
        [timeBtn addTarget:self action:@selector(clickMove:) forControlEvents:UIControlEventTouchUpInside];
        [_topView addSubview:timeBtn];
        
        //中线
        int middleViewY = 9;
        int middleViewH = _topView.height - 2 * middleViewY;
        UIView * middleView = [[UIView alloc] init];
        middleView.backgroundColor = getColor(@"d2d2d2");
        middleView.frame = CGRectMake(SCREEN_WIDTH / 2, middleViewY, 1, middleViewH);
        [_topView addSubview:middleView];
        
    }return _topView;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.topView.height, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49 - self.topView.height) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = getColor(@"e5e5e5");
//        _tableView.contentInset = UIEdgeInsetsMake(10, 0, 0, 0);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        UIView *clearView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 10)];
        clearView.backgroundColor = [UIColor clearColor];
        _tableView.tableHeaderView = clearView;
        WS(ws)
        self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            ws.currentPage ++;
            [self getCityTravelData];
        }];
        
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            ws.currentPage = 1;
            [self getCityTravelData];
        }];
        [self.tableView.mj_header beginRefreshing];
        
        
    }return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - action
- (void)addTravelNotePage{
    if ([UserModel isSaveLogin]) {
        NewTravelNoteViewController * newTravelNoteVC = [[NewTravelNoteViewController alloc] init];
        newTravelNoteVC.navigationItem.title = @"新建游记";
        [self.navigationController pushViewController:newTravelNoteVC animated:YES];
    }else{
        [AppHelper showLogin];
    }
}

- (void)searchTravelPage{
    SearchTravelViewController * searchTravelVC = [[SearchTravelViewController alloc] init];
    [self.navigationController pushViewController:searchTravelVC animated:YES];
}

- (void)clickMove:(UIButton *)sender
{
    if (sender.frame.origin.x + 1 > SCREEN_WIDTH / 2) {
        _isPopular = NO;
    }else{
        _isPopular = YES;
    }
    [self.tableView.mj_header beginRefreshing];
    WS(ws)
    [UIView animateWithDuration:0.3 animations:^{
        ws.leftView.x = sender.x + 38;
    }];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * cellID = @"cell";
    [self.tableView registerClass:[CityTravelTableViewCell class] forCellReuseIdentifier:cellID];
    CityTravelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    if (!cell) {
        cell = [[CityTravelTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.modelData = self.dataArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 220 / WIDTH_5S_SCALE;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([UserModel isSaveLogin]) {
        TravelsViewController * travelsVC = [[TravelsViewController alloc] init];
        CityTravelTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        self.travelID = [cell.modelData.diaryId intValue];
        travelsVC.dID = self.travelID;
        //    NSLog(@"发现活动ID%i",self.travelID);
        [self.navigationController pushViewController:travelsVC animated:YES];
    }else{
        [AppHelper showLogin];
    }
}

@end
